package com.hhh.tools.ui.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.hhh.tools.ui.components.AppTitle
import com.hhh.tools.ui.theme.ComposeDemoTheme

@Composable
fun BaseConversionPage(navController: NavController) {
    ComposeDemoTheme {
        var binText by remember { mutableStateOf("") }
        var octText by remember { mutableStateOf("") }
        var decText by remember { mutableStateOf("") }
        var hexText by remember { mutableStateOf("") }
        Column(
            Modifier
                .background(MaterialTheme.colors.background)
                .fillMaxSize()
        ) {
            AppTitle("进制转换") {
                navController.popBackStack()
            }

            Spacer(modifier = Modifier.height(10.dp))

            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text("二进制", modifier = Modifier.padding(10.dp, 0.dp))

                TextField(
                    value = binText,
                    onValueChange = {
                        binText = it
                    },
                    textStyle = TextStyle(textAlign = TextAlign.End),
                    maxLines = 1,
                    singleLine = true,
                    placeholder = {
                        Text(
                            text = "1010",
                            textAlign = TextAlign.End,
                            modifier = Modifier.fillMaxWidth()
                        )
                    },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(0.dp, 0.dp, 10.dp, 0.dp)
                )
            }

            Spacer(modifier = Modifier.height(10.dp))

            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text("八进制", modifier = Modifier.padding(10.dp, 0.dp))

                TextField(
                    value = octText,
                    onValueChange = {
                        octText = it
                    },
                    textStyle = TextStyle(textAlign = TextAlign.End),
                    maxLines = 1,
                    singleLine = true,
                    placeholder = {
                        Text(
                            text = "12",
                            textAlign = TextAlign.End,
                            modifier = Modifier.fillMaxWidth()
                        )
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(0.dp, 0.dp, 10.dp, 0.dp)
                )
            }

            Spacer(modifier = Modifier.height(10.dp))

            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text("十进制", modifier = Modifier.padding(10.dp, 0.dp))

                TextField(
                    value = decText,
                    onValueChange = {
                        decText = it
                    },
                    textStyle = TextStyle(textAlign = TextAlign.End),
                    maxLines = 1,
                    singleLine = true,
                    placeholder = {
                        Text(
                            text = "10",
                            textAlign = TextAlign.End,
                            modifier = Modifier.fillMaxWidth()
                        )
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(0.dp, 0.dp, 10.dp, 0.dp)
                )
            }

            Spacer(modifier = Modifier.height(10.dp))

            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text("十六进制", modifier = Modifier.padding(10.dp, 0.dp))

                TextField(
                    value = hexText,
                    onValueChange = {
                        hexText = it
                    },
                    textStyle = TextStyle(textAlign = TextAlign.End),
                    maxLines = 1,
                    singleLine = true,
                    placeholder = {
                        Text(
                            text = "A",
                            textAlign = TextAlign.End,
                            modifier = Modifier.fillMaxWidth()
                        )
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(0.dp, 0.dp, 10.dp, 0.dp)
                )
            }
        }
    }
}

fun getValidatedNumber(text: String): String {
    // Start by filtering out unwanted characters like commas and multiple decimals
    val filteredChars = text.filterIndexed { index, c ->
        c in "0123456789" ||                      // Take all digits
                (c == '.' && text.indexOf('.') == index)  // Take only the first decimal
    }
    // Now we need to remove extra digits from the input
    return if(filteredChars.contains('.')) {
        val beforeDecimal = filteredChars.substringBefore('.')
        val afterDecimal = filteredChars.substringAfter('.')
        beforeDecimal.take(3) + "." + afterDecimal.take(2)    // If decimal is present, take first 3 digits before decimal and first 2 digits after decimal
    } else {
        filteredChars.take(3)                     // If there is no decimal, just take the first 3 digits
    }
}

@Preview
@Composable
fun BaseConversionPagePreview() {
    val navController = rememberNavController()
    BaseConversionPage(navController)
}

